﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using Catering.View_Operaction;
using System.Text;
using System.IO;
using Aspose.Words;

namespace Catering
{
    public partial class index : UserIsNull
    {
        // 属性
        Catering_Rules.Announcement Ann = new Catering_Rules.Announcement();
        public DataTable dt_jinjie = new DataTable();
        public DataTable dt_weixiu = new DataTable();
        public DataTable dt_aqyh = new DataTable();
        public DataTable dt_slys = new DataTable();
        public string UserSession = "";//判断是否有删除功能权限
        public string UserID = "";
        public double rand = new Random().NextDouble();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["PersonnelID"] != null && Session["PersonnelID"].ToString() != "") UserSession = Bind_Data_to_Controls.Encode(Session["PersonnelID"].ToString());
            if (Session["PersonnelID"] == null || Session["PersonnelID"].ToString() == "")
            {
                //Response.Redirect("login.aspx");
            }
            else
            {
                UserID = Session["PersonnelID"].ToString();
            }


            if (!IsPostBack)
            {
                this.NoticeRept.DataSource = Ann.GetList(10, "", "Release desc").Tables[0];
                this.NoticeRept.DataBind();
            }
            this.LoadUsersCount();
            this.lab_time.Text = DateTime.Now.Year.ToString() + "年" + DateTime.Now.Month.ToString() + "月" + DateTime.Now.Day.ToString() + "日";
            this.lab_week.Text = this.WeekBing(DateTime.Now.DayOfWeek);
            this.lab_username.Text = Session["User"].ToString();

            int companyid = 0;
            int groupid = 0;
            int classid = 0;
            bool isAdmin = false;
            Bind_Data_to_Controls.GetCompanyByCurrentUser(Session["PersonnelID"].ToString(), out companyid, out groupid, out classid, out isAdmin);

            this.LoadJinJie(isAdmin, classid);
            this.Loadweixiu(isAdmin, classid);
            this.LoadAQYH(isAdmin, classid);
            this.LoadSLYS(isAdmin, classid);
        }

        //加载警戒数据
        public void LoadJinJie(bool isAdmin, int classid)
        {
            dt_jinjie = new DataTable();
            dt_jinjie.Columns.Add("RawID");
            dt_jinjie.Columns.Add("RawName");
            dt_jinjie.Columns.Add("Date");
            dt_jinjie.Columns.Add("CName");
            dt_jinjie.Columns.Add("Content");
            string tempwhere = "";
            if (!isAdmin) tempwhere = " ClassID=" + classid + " ";
            DataTable dt_RoomWaing = new Catering_Rules.RoomWaing().GetList(tempwhere).Tables[0];
            foreach (DataRow row_RoomWaing in dt_RoomWaing.Rows)
            {
                int ClassID = Convert.ToInt32(row_RoomWaing["ClassID"].ToString());
                int RawID = Convert.ToInt32(row_RoomWaing["RAWID"].ToString());
                decimal MinNumber = Convert.ToDecimal(row_RoomWaing["MinContent"].ToString().SetToString());//库存警戒线
                decimal MaxNumber = Convert.ToDecimal(row_RoomWaing["MaxContent"].ToString().SetToString());//最大容量

                Catering_Model.ClassInfo ClassModel = new Catering_Rules.ClassInfo().GetModel(ClassID);
                string ClassName = ClassModel == null ? "" : ClassModel.CName;

                List<ArrayList> al_KF_List = new Catering_Rules.BalanceInfo().GetLastBalanceInfoList(ClassID.ToString(), RawID.ToString());

                if (al_KF_List != null && al_KF_List.Count > 0)
                {
                    foreach (ArrayList al_KF in al_KF_List)
                    {
                        decimal jieyu = Convert.ToDecimal(al_KF[1]);
                        if (jieyu < MinNumber)
                        {
                            DataRow row_jinjie = dt_jinjie.NewRow();
                            row_jinjie["RawID"] = al_KF[0];
                            row_jinjie["RawName"] = new Catering_Rules.T_RawMaterial().GetModel(Convert.ToInt32(RawID.ToString())).RAWName;
                            row_jinjie["Date"] = DateTime.Now.ToShortDateString();
                            row_jinjie["CName"] = ClassName;
                            row_jinjie["Content"] = "低于库存警戒线";
                            dt_jinjie.Rows.Add(row_jinjie);
                        }

                        if (jieyu > MaxNumber)
                        {
                            DataRow row_jinjie = dt_jinjie.NewRow();
                            row_jinjie["RawID"] = al_KF[0];
                            row_jinjie["RawName"] = new Catering_Rules.T_RawMaterial().GetModel(Convert.ToInt32(RawID.ToString())).RAWName;
                            row_jinjie["Date"] = DateTime.Now.ToShortDateString();
                            row_jinjie["CName"] = ClassName;
                            row_jinjie["Content"] = "超过最大库存量";
                            dt_jinjie.Rows.Add(row_jinjie);
                        }
                    }
                }
            }
        }

        //加载维修报警
        public void Loadweixiu(bool isAdmin, int classid)
        {
            string tempwhere = "";
            if (!isAdmin) tempwhere = " AND ClassID=" + classid + " ";
            this.dt_weixiu = new Catering_Rules.DailyManagement().GetList("Repair!='' and LTRIM(Repair) NOT LIKE '无事%' and year([Date])=" + DateTime.Now.Year + " " + tempwhere + " order by [Date] desc").Tables[0];
        }

        public void LoadAQYH(bool isAdmin, int classid)
        {
            string tempwhere = "";
            if (!isAdmin) tempwhere = " AND ClassID=" + classid + " ";
            this.dt_aqyh = new Catering_Rules.DailyManagement().GetList("temp2 IS NOT NULL AND temp2!='' and LTRIM(temp2) NOT LIKE '无事%' and year([Date])=" + DateTime.Now.Year + " " + tempwhere + " order by [Date] desc").Tables[0];
        }

        public void LoadSLYS(bool isAdmin, int classid)
        {
            string tempwhere = "";
            if (!isAdmin) tempwhere = " AND ClassID=" + classid + " ";
            this.dt_slys = new Catering_Rules.DailyManagement().GetList("DishTest IS NOT NULL AND DishTest!='' and LTRIM(DishTest) NOT LIKE '合格%'  and year([Date])=" + DateTime.Now.Year + " " + tempwhere + " order by [Date] desc").Tables[0];
        }

        //绑定星期对应的日期
        public string WeekBing(DayOfWeek date)
        {
            switch (date)
            {
                case DayOfWeek.Friday:
                    return "星期五";
                    break;
                case DayOfWeek.Monday:
                    return "星期一";
                    break;
                case DayOfWeek.Saturday:
                    return "星期六";
                    break;
                case DayOfWeek.Sunday:
                    return "星期日";
                    break;
                case DayOfWeek.Thursday:
                    return "星期四";
                    break;
                case DayOfWeek.Tuesday:
                    return "星期二";
                    break;
                case DayOfWeek.Wednesday:
                    return "星期三";
                    break;
            }
            return "";
        }

        public void LoadUsersCount()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("UserName");
            Hashtable ht = (Hashtable)Application["UsersCount"];
            foreach (DictionaryEntry item in ht)
            {
                string[] value = item.Value.ToString().Split(',');
                DataRow row = dt.NewRow();
                row["UserName"] = item.Value;
                if (value.Length > 1)
                {
                    row["UserName"] = value[0];
                }
                dt.Rows.Add(row);
            }
            this.rep_userscount.DataSource = dt;
            this.rep_userscount.DataBind();
            this.lab_count.Text = dt.Rows.Count.ToString();
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("Catering_Page/Frame/Main.aspx?topurl=Top_Mes.aspx&mainurl=Mes/SMSHall.aspx&mstatus=1");
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            string tmppath = Server.MapPath("HelperFile/userhelper.doc");
            Document doc = new Document(tmppath); //载入模板   
            doc.Save("操作说明书.doc", SaveFormat.Doc, SaveType.OpenInWord, Response);
        }

        /// <summary>
        /// 字符串格式化
        /// </summary>
        /// <param name="oldStr">要格式化的字符</param>
        /// <param name="num">保留字数</param>
        /// <returns></returns>
        public string GetSubStringStr(string oldStr, int num)
        {
            if (oldStr.Length <= (num + 1)) return oldStr;
            return oldStr.Substring(0, num) + "…";
        }

        public string GetNewsMesssage()
        {
            int num = new Catering_Rules.SMS_MsgBLL().GetUNReadCount(Session["UserID"].ToString());
            if (num > 0) return "<image src='Images/newsm.gif' style='vertical-align:middle;' alt='您有" + num + "条新消息' />";
            return "<image src='Images/newsm.png' style='vertical-align:middle;'/>";
        }
    }
}